package com.sdy.config;

import com.demo.blog.Blog;
import com.demo.blog.BlogController;
import com.demo.index.IndexController;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;
import com.sdy.controller.DepartmentController;
import com.sdy.controller.EmployerController;
import com.sdy.controller.ModelInfoController;
import com.sdy.controller.PositionController;
import com.sdy.controller.SoundcodeController;
import com.sdy.interceptor.LoginInterceptor;
import com.sdy.model.Department;
import com.sdy.model.Employer;
import com.sdy.model.Messageboard;
import com.sdy.model.Modeinfo;
import com.sdy.model.Modeltype;
import com.sdy.model.Position;
import com.sdy.model.Soundcode;

/**
	author:LJQ
	date:2014-11-30
 */
public class MainConfig extends JFinalConfig {
	
	private static String url ;
	private static String username ;
	private static String password ;
	
	static{
		if(isLocal()){
			url = Constant.LOCAL_URL;
			username = Constant.LOCAL_USERNAME;
			password = Constant.LOCAL_PASSWORD;
		}else{
			url = Constant.WEB_URL;
			username = Constant.WEB_USERNAME;
			password = Constant.WEB_PASSWORD;
		}
	}


	@Override
	public void configConstant(Constants me) {
		me.setDevMode(true);
		me.setError404View("404.html");
		me.setViewType(ViewType.JSP);
	}

	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		me
		.add("/admin",EmployerController.class)
		.add("/admin/modeinfo",ModelInfoController.class)
		.add("/admin/soundcode",SoundcodeController.class)
		.add("/admin/department", DepartmentController.class)
		.add("/admin/position", PositionController.class);
		me.add("/", IndexController.class, "/index");	// 第三个参数为该Controller的视图存放路径
		me.add("/index",ModelInfoController.class)
		  .add("/index/employer", EmployerController.class)
		.add("/index/code", SoundcodeController.class);
		me.add("/blog", BlogController.class);			// 第三个参数省略时默认与第一个参数值相同，在此即为 "/blog"
	}

	@Override
	public void configPlugin(Plugins me) {
		C3p0Plugin c3 = new C3p0Plugin(url, username, password);
		me.add(c3);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3);
		me.add(arp);
		arp.addMapping("blog", Blog.class);	// 映射blog 表到 Blog模型
		arp.addMapping("department", Department.class);
		arp.addMapping("employer", Employer.class);
		arp.addMapping("messageboard", Messageboard.class);
		arp.addMapping("modelinfo", Modeinfo.class);
		arp.addMapping("modeltype",Modeltype.class);
		arp.addMapping("soundcode",Soundcode.class);
		arp.addMapping("position", Position.class);
		arp.setShowSql(true);
//		EhCachePlugin eh = new EhCachePlugin();
//		me.add(eh);
	}
	
	private static boolean isLocal(){
		boolean flag = false;
		String system = System.getProperty("os.name");
		if(system.startsWith("Windows")){
			flag = true;
		}
		return flag;
	}


	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		//me.add(new SessionInViewInterceptor());
		me.add(new LoginInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub

	}

}